#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <stdint.h>
#include <amqp.h>
#include <amqp_framing.h>
#include "utils.h"

int main(int argc, char const *argv[])
{
	amqp_connection_state_t conn;
	char *hostname = "localhost"; //argv[1];
	int port = 5672; //atoi(argv[2]);
	char *exchange = "nam_topic";
	char *exchangetype = "topic";
	int sockfd;

	conn = amqp_new_connection();
	sockfd = amqp_open_socket(hostname, port);
	amqp_set_sockfd(conn, sockfd);
	amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest");
	
	amqp_channel_open(conn, 1);
	amqp_get_rpc_reply(conn);
	
	amqp_exchange_declare(conn, 1, amqp_cstring_bytes(exchange),
		amqp_cstring_bytes(exchangetype), 0, 0, amqp_empty_table);
	amqp_get_rpc_reply(conn);

	char *routing_key = "nam.*.test";
	char *routing_key2 = "nam.abc.test";

	char message[256] = {0};

  amqp_queue_declare(conn, 1, amqp_cstring_bytes("nam_queue"), 0, 0, 0, 1, amqp_empty_table);

	amqp_queue_bind(conn, 1, amqp_cstring_bytes("nam_queue"), amqp_cstring_bytes(exchange), amqp_cstring_bytes(routing_key), amqp_empty_table);


  amqp_queue_declare(conn, 1, amqp_cstring_bytes("nam_queue2"), 0, 0, 0, 1, amqp_empty_table);

	amqp_queue_bind(conn, 1, amqp_cstring_bytes("nam_queue2"), amqp_cstring_bytes(exchange), amqp_cstring_bytes(routing_key2), amqp_empty_table);

	// amqp_basic_properties_t props;
	// props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
	// props.content_type = amqp_cstring_bytes("text/plain");
	// props.delivery_mode = 2;
	//state, channel, exchange, routing_key, mandatory, immediate, properties, body

	int i=0;
	while(1)
	{ 
		sprintf(message, "[%d]-%s\n", i++, "hi there");
		printf("wait...\n");
		amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(routing_key), 0, 0, NULL, amqp_cstring_bytes(message));
		amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange), amqp_cstring_bytes(routing_key2), 0, 0, NULL, amqp_cstring_bytes(message));
		sleep(1);
	}
	// amqp_bytes_t message_bytes;
	//amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS);
	//amqp_connection_close(conn, AMQP_REPLY_SUCCESS);
	//amqp_destroy_connection(conn);
	return 0;
}
